package com.zb.controller;

import com.zb.entity.TbOrderItemModel;
import com.zb.service.CartService;
import com.zb.util.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/cart")
@RestController
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("/add")
    public Boolean addCart(@RequestParam("skuId") String skuId, @RequestParam("num") Integer num) {
        String username = TokenDecode.getUserInfo().get("username");

        return cartService.addItem(username, skuId, num);
    }

    @GetMapping("/list")
    public List<TbOrderItemModel> list() {

        String username = TokenDecode.getUserInfo().get("username");
        return cartService.list(username);
    }

    @GetMapping("/del")
    public Boolean remove(@RequestParam("skuId") String skuId) {
        String username = TokenDecode.getUserInfo().get("username");

        return cartService.remove(username, skuId);


    }

    @GetMapping("/select")
    public List<TbOrderItemModel> select(@RequestParam("skuIds") String[] skuIds) {

        String username = TokenDecode.getUserInfo().get("username");
        List<TbOrderItemModel> select = cartService.select(skuIds, username);
        return select;
    }
}